package com.zhoug.common.utils;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * RecyclerView 工具类
 *
 * @Author 35574
 * @Date 2020/9/29
 * @Description
 */
public class RecyclerViewUtils {
    private static final String TAG = ">>>RecyclerViewUtils";
    private static final boolean DEBUG = false;

    /**
     * 判断recyclerView是否滚动到顶部
     *
     * @param recyclerView
     * @return true:滚动到顶部或者不能滚动
     */
    public static boolean isScrollTop(RecyclerView recyclerView) {
        int offset = recyclerView.computeVerticalScrollOffset();
        int extent = recyclerView.computeVerticalScrollExtent();
        int range = recyclerView.computeVerticalScrollRange();
        if (DEBUG) {
            LogUtils.d(TAG, "onScrollStateChanged:offset=" + offset);//是当前屏幕之前滑过的距离
            LogUtils.d(TAG, "onScrollStateChanged:extent=" + extent);//当前屏幕显示的区域高度
            LogUtils.d(TAG, "onScrollStateChanged:range=" + range);//整个View控件的高度,上面滚动出屏幕+中间显示+下面待进入屏幕
        }
        //不能滚动
        if (range == extent) {
            return true;
        } else return offset <= 0;

    }

    /**
     * 判断是否滚动到底部
     *
     * @param recyclerView
     * @return true:滚动到底部或者不能滚动
     */
    public static boolean isScrollBottom(RecyclerView recyclerView) {
        int offset = recyclerView.computeVerticalScrollOffset();
        int extent = recyclerView.computeVerticalScrollExtent();
        int range = recyclerView.computeVerticalScrollRange();
        if (DEBUG) {
            LogUtils.d(TAG, "onScrollStateChanged:offset=" + offset);//是当前屏幕之前滑过的距离
            LogUtils.d(TAG, "onScrollStateChanged:extent=" + extent);//当前屏幕显示的区域高度
            LogUtils.d(TAG, "onScrollStateChanged:range=" + range);//整个View控件的高度,上面滚动出屏幕+中间显示+下面待进入屏幕
        }
        //不能滚动
        if (range == extent) {
            return true;
        } else return offset + extent >= range - 1;//-1为了精度缺失
    }


    /**
     * 是否能滚动
     *
     * @param recyclerView
     * @return
     */
    public static boolean canScroll(RecyclerView recyclerView) {
        if (recyclerView != null) {
            return (recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollExtent() != 0);
        }
        return false;
    }

    /**
     * 滚动到指定的position
     *
     * @param recyclerView
     * @param position
     */
    public static void scrollTo(RecyclerView recyclerView, int position) {
        if (position >= 0 && recyclerView != null) {
            try {
                recyclerView.scrollToPosition(position);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(position, 0);
                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                    staggeredGridLayoutManager.scrollToPositionWithOffset(position, 0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 动画滚动到指定的position
     *
     * @param recyclerView
     * @param position
     */
    public static void smoothScrollTo(RecyclerView recyclerView, int position) {
        if (position >= 0 && recyclerView != null) {
            try {
//                recyclerView.smoothScrollToPosition(position);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager != null) {
                    layoutManager.smoothScrollToPosition(recyclerView, null, position);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    /**
     * 根据坐标位置xy获取触摸点的item的position
     * @param recyclerView
     * @param x
     * @param y
     * @return
     */
    public static int getChildPositionByXY(RecyclerView recyclerView, float x, float y) {
        if (recyclerView != null) {
            View child = recyclerView.findChildViewUnder(x, y);
            if (child != null) {
                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
                return vh.getAdapterPosition();
            }
        }
        return -1;
    }


}
